﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Rhino.Mocks;
using SimpleServiceBus.Bus;
using SimpleServiceBus.Bus.Pipeline;
using SimpleServiceBus.Bus.Pipeline.PipelineComponents;

namespace SimpleServiceBus.Tests.Bus.Pipeline.PipelineComponents
{
    [TestFixture]
    public class PipelineComponentBaseTestFixture
    {
        [Test]
        public void Test_Component_Gets_Default_Name_And_Is_Enabled()
        {
            var comp = new TestPipelineComponent();
            Assert.That(comp.Name, Is.EqualTo(comp.GetType().Name));
            Assert.IsTrue(comp.Enabled);
            Assert.IsTrue(comp.EnabledFor(null));
        }

        [Test][ExpectedException(typeof(InvalidOperationException))]
        public void Wont_Process_Without_Pipeline()
        {
            var comp = new TestPipelineComponent();
            comp.ProcessMessage(new MessageEnvelope {Body = 1});

        }

        [Test]
        public void Will_Call_Abstract_Process_Method()
        {
            var comp = new TestPipelineComponent();
            var pipeline = MockRepository.GenerateStub<IMessagePipeline>();
            var env = new MessageEnvelope {Body = "Hello"};
            IMessageContext context = new MessageContext(env,pipeline);
            pipeline.Stub(action => action.CurrentContext).Return(context);
            comp.Pipeline = pipeline;

            comp.ProcessMessage(env);

            Assert.AreSame(TestPipelineComponent.LastEnvelope, env);
            Assert.AreSame(TestPipelineComponent.LastContext, context);
        }
    }

    public class TestPipelineComponent : PipelineComponentBase
    {
        public static MessageEnvelope LastEnvelope;
        public static IMessageContext LastContext;
        public static void Reset()
        {
            LastEnvelope = null;
            LastContext = null;
        }

        protected override void ProcessMessage(MessageEnvelope messageEnvelope, IMessageContext context)
        {
            LastEnvelope = messageEnvelope;
            LastContext = context;
        }
    }
}
